package com.uhealin;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.hive.jdbc.HiveDriver;
import java.text.MessageFormat;


public class ImportSample {

	
	public static void main(String[] args) throws ClassNotFoundException, SQLException{
		
		Class.forName(HiveDriver.class.getName());
		
		String host=args[0]
		,inpath=args[2]
		,user = "hive"
        , password = "0000"
		,db="jdbc_demo"
		,table=new File(inpath).getName().replace(".", "_");
		;
		
                System.out.println("inpath:"+inpath+",table:"+table);
	       
		String url=MessageFormat.format("jdbc:hive2://localhost:10000/default", host);		
                	
		Connection con=DriverManager.getConnection(url,user,password);
		
		Statement stmt=con.createStatement();
		
           
		String sql="create database "+db;
	        
                try{	
		stmt.execute(sql);
		
		sql="use "+db;
		
		stmt.execute(sql);
                }catch(SQLException sqe){}		

                try{
		sql=MessageFormat.format("create table {0} (full_name string)", table);
		
                System.out.println(sql);

		stmt.execute(sql);
		}catch(SQLException sqe){}finally{}

		sql=MessageFormat.format("load data local inpath ''{0}'' into table {1}", inpath,table);
                
                System.out.println(sql);		
		stmt.execute(sql);
	}
	
}
